package com.boardgame.gwt.service.operate.action;
import com.boardgame.common.entity.ResponseInfo;
import com.boardgame.gwt.data.GwtGameBoard;
import com.boardgame.gwt.data.GwtPlayerInfo;
import com.boardgame.gwt.finalinfo.GameStage;
import com.boardgame.gwt.finalinfo.SyntaxMatchFinal;
import com.boardgame.gwt.service.operate.SyntaxBaseAction;
import com.boardgame.gwt.service.operate.SyntaxRequest;
import com.boardgame.gwt.data.space.GwtSpace;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 移动
 * @author henry
 */
@Component
public class MoveAction extends SyntaxBaseAction {


    @Override
    public Pattern getPattern() {
        return Pattern.compile(SyntaxMatchFinal.MOVE_ACTION);
    }

    //之前的地点
    private GwtSpace gwtSpaceOld;
    @Override
    public ResponseInfo action(GwtGameBoard gwtGameBoard, SyntaxRequest syntaxRequest, Matcher matcher) {
        super.action(gwtGameBoard, syntaxRequest, matcher);
        //判断是不是移动阶段
        if (super.getGwtGameBoard().getGameStage()!=GameStage.STAGE_MOVE){
            return ResponseInfo.Failed("当前不是移动阶段");
        }
        //当前玩家
        GwtPlayerInfo gwtPlayer = super.gwtGameBoard.getCurrentPlayer();
        //移动玩家到新的地点
        GwtSpace gwtSpace = super.getGwtSplace(Integer.parseInt(matcher.group(1)),Integer.parseInt(matcher.group(2)));
        if (gwtSpace==null){
            return ResponseInfo.Failed("不存在的地点");
        }
        //如果是第一回合
        if (super.gwtGameBoard.getGameRound()==1){
            if (gwtSpace.getGwtSpaceTile()==null){
                return ResponseInfo.Failed("移动必须选择非空建筑!");
            }
            //剩余一步，后面会扣减
            gwtPlayer.setMovedSurplus(1);
        }
        else{
            this.gwtSpaceOld = null;
            //将玩家从之前的地点移除
            super.gwtGameBoard.getListSpace().stream().filter(oldSpace -> {
                //有用户的格子
                return oldSpace.getListPlayer()!=null && oldSpace.getListPlayer().size()>0;
            }).forEach(space -> {
                //删除当前用户
                for(GwtPlayerInfo gwtPlayerInfo:space.getListPlayer()){
                    if (gwtPlayerInfo.getUserName().equals(gwtPlayer.getUserName())){
                        this.gwtSpaceOld = space;
                        space.getListPlayer().remove(gwtPlayerInfo);
                        break;
                    }
                };
            });
            if (!this.checkSpaceDistance(gwtPlayer,gwtSpace)){
                return ResponseInfo.Failed("请检查移动地点，目前系统只能一步一步移动!");
            }
            else if (super.gwtGameBoard.getGameRound()!=1 && gwtPlayer.getMovedSurplus()==0){
                return ResponseInfo.Failed("剩余移动步数为0，无法再次移动!");
            }
        }
        //移动成功
        //添加用户到空格
        gwtSpace.addPlayer(gwtPlayer);
        //如果移动到的板块有任务板块，扣减移动步数
        if (gwtSpace.getGwtSpaceTile()!=null){
            gwtPlayer.setMovedSurplus(gwtPlayer.getMovedSurplus()-1);
        }
        //如果移动步数==0，进入行动阶段
        if (gwtPlayer.getMovedSurplus()==0){
            this.getGwtGameBoard().setGameStage(GameStage.STAGE_ACTION);
        }
        return ResponseInfo.Success(null);
    }

    /**
     * 检查距离是否合法
     * @param newSpace
     */
    private boolean checkSpaceDistance(GwtPlayerInfo gwtPlayer,GwtSpace newSpace){
        //如果还没有进入游戏，且游戏刚开始，随便移动
        if (this.gwtSpaceOld==null){
            return true;
        }
        else{
            //if (this.gwtSpaceOld.getRow() == 0 && this.gwtSpaceOld.getColumn()==5)
            GwtSpace nextSpace = super.getGwtSplace(this.gwtSpaceOld.getRow(),this.gwtSpaceOld.getColumn()+1);
            //如果为空，代表这一行没有其他的了
            if (nextSpace==null){
                //新行必须移动到第一格
                if (newSpace.getColumn()!=0){
                    return false;
                }
                int[][] list = {
                        {0,2},{1,2},{2,3},{2,4},{3,5},{4,5},{5,6},{5,7},{5,8},{6,9},{7,9},{8,9},{9,10},{9,11},
                        {10,12},{10,12},{12,13},{12,14},
                };
                return Arrays.stream(list).anyMatch(item->item[0]==this.gwtSpaceOld.getRow()&&item[1]==newSpace.getRow());
            }
            else{
                return newSpace.getRow() == nextSpace.getRow() && newSpace.getColumn() == nextSpace.getColumn();
            }
        }
    }
}
